package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import cn.tedu.egweibo1.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo1.pojo.vo.WeiboIndexVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

//    发微博
    @PostMapping("insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO = (UserVO)session.getAttribute("user");
        System.out.println("weiboMapper = " + weiboDTO + ", session = " + userVO);
        if (userVO==null){
            return 2;
        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }
//    查看微博
    @RequestMapping("sekectIndex")
    public List<WeiboIndexVO> sekectIndexWeibo(){

        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndexweibo();
        System.out.println(weiboIndexVOS);
        return weiboIndexVOS;
    }

//    微博详情
    @GetMapping("selectByIdi/{id}")
    public WeiboDetailVO weiboDetailVO(@PathVariable Integer id){

        return weiboMapper.selectByIdWeibo(id);
    }
}
